<?php

class GPaginate {

    private $_id;
    private $_header;
    private $_showHeader = false;
    private $_showSearch = false;
    private $_footer;
    private $_title; // Título da paginação
    private $_post; // Arquivo que será enviada a requisição ajax
    private $_rp = 1; // Registros por página
    private $_start = 1; // Início da página
    private $_count = 1; //Quantidade de links total
    private $_display = 5; //Quantidade de links exibido na paginação
    private $_page = 1; //Página inicial
    private $_columns = array();
    private $_param = "undefined";

    /**
     *
     * @param String $id
     * @param String $post
     * @param String $title
     */
    public function __construct($id, $post, $title) {
        $this->_id = $id;
        $this->_post = $post;
        $this->_title = $title;
    }

    /**
     *
     * @param String $display Nome de exibição do campo
     * @param String $name Nome do campo no banco
     * @param String $width Largura da coluna em pixel
     * @param String $sortable Permite habilitar ordenação | Valores:['true','false'] | Default: false
     * @param String $align Alinhamento do valor na celula | Valores:['left','center','right'] | Default: left
     */
    function setCol($display, $name, $width, $sortable = 'false', $align = 'left') {
        $this->_columns[] = array($display, $name, $width, $sortable, $align);
    }

    function makeHeader() {
        $header = '<table width="100%" class="__paginateHeader">';
        $header .= '<tr>';
        $header .= '<td colspan="' . count($this->_columns) . '">';

        if (!empty($this->_title))
            $header .= '<div class="__paginateTitle">' . $this->_title . '</div>';

        if ($this->_showSearch) {
            $header .= '<div class="__paginateSearch">';
            $header .= '<div class="__divTxtBuscar"><input type="text" id="__txt_search_' . $this->_id . '" maxlenght="100" class="__txtBuscar" /></div>';
            $header .= '<div class="__divBtnBuscar"><input type="button" id="__btn_search_' . $this->_id . '" value="Buscar" class="__btnBuscar botao" /></div>';
            $header .= '</div>';
        }
        $header .= '</td>';
        $header .= '</tr>';
        $header .= '<thead>';
        $header .= '<tr>';
        foreach ($this->_columns as $col) {
            $titleC = ($col[3] == 'true') ? '<a class="__sortable" id="' . $col[1] . '" rel="ASC">' . $col[0] . '</a>' : $col[0];
            $header .= '<th width="' . $col[2] . '" align="' . $col[4] . '">' . $titleC . '</th>';
        }
        $header .= '</tr>';
        $header .= '</thead>';
        $header .= '</table>';
        return $header;
    }

    public function get() {
        $pag = '';
        if ($this->_showHeader)
            $pag .= $this->makeHeader();

        $pag .= '<div id="target_' . $this->_id . '" class="__paginateBody"></div>';
        $pag .= $this->_footer;
        $pag .= '<table class="__paginateFooter">';
        $pag .= '<tr><td>';
        $pag .= '<div id="' . $this->_id . '"></div>';
        $pag .= '</td></tr>';
        $pag .= '</table>';

        $vCount = 'count_' . $this->_id;
        $vStart = 'start_' . $this->_id;
        $vDisplay = 'display_' . $this->_id;
        $vRp = 'rp_' . $this->_id;
        $vPost = 'post_' . $this->_id;
        $vTarget = 'target_' . $this->_id;
        $vPage = 'page_' . $this->_id;
        $vQuery = 'query_' . $this->_id;
        $vOrder = 'order_' . $this->_id;
        $vSort = 'sort_' . $this->_id;

        $pag .= '<script>';
        $pag .= 'var ' . $vCount . ' = ' . $this->_count . ';';
        $pag .= 'var ' . $vStart . ' = ' . $this->_start . ';';
        $pag .= 'var ' . $vDisplay . ' = ' . $this->_display . ';';
        $pag .= 'var ' . $vRp . ' = ' . $this->_rp . ';';
        $pag .= 'var ' . $vPost . ' = "' . $this->_post . '";';
        $pag .= 'var ' . $vTarget . ' = "#target_' . $this->_id . '";';
        $pag .= 'var ' . $vPage . ' = "' . $this->_page . '";';
        $pag .= 'var ' . $vQuery . ' = "";';
        $pag .= 'var ' . $vOrder . ' = "";';
        $pag .= 'var ' . $vSort . ' = "";';
        $pag .= 'jQuery(document).ready(function(){';
//        $pag .= 'jQuery(".__sortable").click(function(){';
//        $pag .= '' . $vOrder . ' = jQuery(this).attr("id");';
//        $pag .= '' . $vSort . ' = jQuery(this).attr("rel");';
//        $pag .= 'if(' . $vSort . ' == "ASC") jQuery(this).attr("rel", "DESC");';
//        $pag .= 'else jQuery(this).attr("rel", "ASC");';
//        $pag .= 'jQuery.gAjax.exec(' . $vPost . ',{tipo:"C",count:' . $vCount . ',rp:' . $vRp . ',query:' . $vQuery . ', order: ' . $vOrder . ', sort: ' . $vSort . '},"__callback_' . $this->_id . '(json);","",false);';
//        $pag .= '});';
//        $pag .= 'pressEnter("#__txt_search_' . $this->_id . '","jQuery(\'#__btn_search_' . $this->_id . '\').click()");';
//        $pag .= 'jQuery("#__btn_search_' . $this->_id . '").click(function(){';
//        $pag .= '' . $vQuery . ' = jQuery("#__txt_search_' . $this->_id . '").val();';
//        $pag .= 'jQuery.gAjax.exec(' . $vPost . ',{tipo:"C",count:' . $vCount . ',rp:' . $vRp . ',query:' . $vQuery . '},"__callback_' . $this->_id . '(json);","",false);';
//        $pag .= '});';
//        $pag .= 'var hash_' . $this->_id . ' = removeSustenido(location.hash);';
//        $pag .= 'if(hash_' . $this->_id . ' != ""){';
//        $pag .= 'var arrParam  = hash_' . $this->_id . '.split("&");';
//        $pag .= 'if(arrParam[0] !== undefined){';
//        $pag .= 'var arrPag = arrParam[0].split("=");';
//        $pag .= 'if(arrPag[0] == "p" && arrPag[1] != "")';
//        $pag .= '' . $vPage . ' = arrPag[1];';
//        $pag .= '}';
//        $pag .= 'if(arrParam[1] !== undefined){';
//        $pag .= 'var arrQuery = arrParam[1].split("=");';
//        $pag .= 'if(arrQuery[0] == "q" && arrQuery[1] != ""){';
//        $pag .= '' . $vQuery . ' = arrQuery[1];';
//        $pag .= 'jQuery("#__txt_search_' . $this->_id . '").val(' . $vQuery . ');';
//        $pag .= '}';
//        $pag .= '}';
//        $pag .= '}';
        $pag .= 'jQuery.gAjax.exec(' . $vPost . ',jsonConcat({type:"C",count:' . $vCount . ',rp:' . $vRp . ',query:' . $vQuery . '},' . $this->_param . '),"__callback_' . $this->_id . '(json);","",false);';
        $pag .= '});';
        $pag .= 'function __callback_' . $this->_id . '(json){';
        $pag .= '' . $vCount . ' = json.count;';
        $pag .= '__paginateLoad("' . $this->_id . '",' . $vPost . ',' . $vTarget . ',"R",' . $vCount . ',' . $vDisplay . ',' . $vStart . ',' . $vRp . ',' . $vPage . ',' . $vQuery . ',' . $vOrder . ', ' . $vSort . ',' . $this->_param . ');';
        $pag .= '}';
        $pag .= '</script>';

        return $pag;
    }

    public function show() {
        echo $this->get();
    }

    public function get_id() {
        return $this->_id;
    }

    public function set_id($_id) {
        $this->_id = $_id;
    }

    public function get_display() {
        return $this->_display;
    }

    public function set_display($_display) {
        $this->_display = $_display;
    }

    public function get_header() {
        return $this->_header;
    }

    public function set_header($_header) {
        $this->_header = $_header;
    }

    public function get_footer() {
        return $this->_footer;
    }

    public function set_footer($_footer) {
        $this->_footer = $_footer;
    }

    public function get_title() {
        return $this->_title;
    }

    public function set_title($_title) {
        $this->_title = $_title;
    }

    public function get_post() {
        return $this->_post;
    }

    public function set_post($_post) {
        $this->_post = $_post;
    }

    public function get_rp() {
        return $this->_rp;
    }

    public function set_rp($_rp) {
        $this->_rp = $_rp;
    }

    public function get_start() {
        return $this->_start;
    }

    public function set_start($_start) {
        $this->_start = $_start;
    }

    public function get_count() {
        return $this->_count;
    }

    public function set_count($_count) {
        $this->_count = $_count;
    }

    public function get_page() {
        return $this->_page;
    }

    public function set_page($_page) {
        $this->_page = $_page;
    }

    public function get_showHeader() {
        return $this->_showHeader;
    }

    public function set_showHeader($_showHeader) {
        $this->_showHeader = $_showHeader;
    }

    public function get_showSearch() {
        return $this->_showSearch;
    }

    public function set_showSearch($_showSearch) {
        $this->_showSearch = $_showSearch;
    }

    public function get_param() {
        return $this->_param;
    }

    public function set_param($_param) {
        $this->_param = $_param;
    }

}

?>
